package com.apes.fn.fico.debitnote.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.scm.merchant.account.model.MerchantAccountSettlement;
import com.apes.fn.scm.merchant.settlement.model.MerchantSettlement;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.rop.converter.DateUtils;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Objects;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.fico.debitnote.model
 * @ClassName: DeductionDetails
 * @Author: pyb
 * @Description: 扣收明细
 * @CreateTime: 2020-11-10 15:46
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DeductionDetail.CONST_TABLE_NAME)
public class DeductionDetail extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_deduction_detail";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "扣款明细", padding = 8)
    @Comment("编码")
    private String id;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "merchant_settlement_id", referencedColumnName = "id", nullable = false)
    @Comment("对账单")
    private MerchantSettlement merchantSettlement;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "settlement_id", referencedColumnName = "id")
    @Comment("对账单2.0")
    private MerchantAccountSettlement settlement;


    @ManyToOne(optional = false)
    @JoinColumn(name = "business_debit_note_id", referencedColumnName = "id", nullable = false)
    @Comment("预支单")
    private BusinessDebitNote businessDebitNote;


    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "deduction_date")
    @Comment("扣款日期")
    private Date deductionDate;


    @Column(name = "previous_Principal", columnDefinition = "NUMBER(16,2)DEFAULT 0")
    @Comment("还款前本金")
    private double previousPrincipal = 0.0;

    @Column(name = "previous_interest", columnDefinition = "NUMBER(16,2)DEFAULT 0")
    @Comment("还款前利息")
    private double previousInterest = 0.0;


    @Column(name = "repayment_amount", columnDefinition = "NUMBER(16,2)DEFAULT 0")
    @Comment("已扣收金额")
    private double repaymentAmount = 0.0;


    @Column(name = "current_principal", columnDefinition = "NUMBER(16,2)DEFAULT 0")
    @Comment("未扣收金额")
    private double currentPrincipal = 0.0;

    @Column(name = "current_interest", columnDefinition = "NUMBER(16,2)DEFAULT 0")
    @Comment("还款后利息")
    private double currentInterest = 0.0;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date reconcileBeginDate;

    @Transient
    public Date getReconcileBeginDate() {
        if(Objects.isNull(this.settlement)) return null;
        return this.settlement.getReconcileBeginDate() ;
    }

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date reconcileEndDate;

    @Transient
    public Date getReconcileEndDate() {
        if(Objects.isNull(this.settlement)) return null;
        return this.settlement.getReconcileEndDate();
    }

}
